/*   1:    */ package org.intellij.plugins.relaxNG.compact.psi.impl;
/*   2:    */ 
/*   3:    */

import com.intellij.codeInsight.daemon.EmptyResolveMessageProvider;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.LocalQuickFixProvider;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.PsiReferenceBase.Poly;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.ContainerUtil;
import org.intellij.plugins.relaxNG.compact.RncFileType;
import org.intellij.plugins.relaxNG.compact.RncTokenTypes;
import org.intellij.plugins.relaxNG.compact.psi.*;
import org.intellij.plugins.relaxNG.compact.psi.util.EscapeUtil;
import org.intellij.plugins.relaxNG.compact.psi.util.RenameUtil;
import org.intellij.plugins.relaxNG.model.Define;
import org.intellij.plugins.relaxNG.model.resolve.DefinitionResolver;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Set;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */
/*  35:    */
/*  36:    */
/*  37:    */
/*  38:    */
/*  39:    */
/*  40:    */
/*  41:    */
/*  42:    */
/*  43:    */
/*  44:    */
/*  45:    */
/*  46:    */ 
/*  47:    */ class PatternReference
/*  48:    */   extends PsiReferenceBase.Poly<RncRef>
/*  49:    */   implements Function<Define, ResolveResult>, LocalQuickFixProvider, EmptyResolveMessageProvider
/*  50:    */ {
/*  51:    */   public PatternReference(RncRef paramRncRef)
/*  52:    */   {
/*  53: 58 */     super(paramRncRef);
/*  54:    */   }
/*  55:    */   
/*  56:    */   public TextRange getRangeInElement()
/*  57:    */   {
/*  58: 62 */     ASTNode localASTNode = a();
/*  59: 63 */     if (localASTNode == null) {
/*  60: 63 */       return TextRange.from(0, 0);
/*  61:    */     }
/*  62: 64 */     int i = ((RncRef)this.myElement).getTextOffset();
/*  63: 65 */     return TextRange.from(i - ((RncRef)this.myElement).getTextRange().getStartOffset(), localASTNode.getTextLength());
/*  64:    */   }
/*  65:    */   
/*  66:    */   private ASTNode a()
/*  67:    */   {
/*  68: 69 */     ASTNode localASTNode = ((RncRef)this.myElement).getNode();
/*  69: 70 */     assert (localASTNode != null);
/*  70: 71 */     return localASTNode.findChildByType(RncTokenTypes.IDENTIFIERS);
/*  71:    */   }
/*  72:    */   
/*  73:    */   @Nullable
/*  74:    */   public PsiElement resolve()
/*  75:    */   {
/*  76: 76 */     ResolveResult[] arrayOfResolveResult = multiResolve(false);
/*  77: 77 */     return arrayOfResolveResult.length == 1 ? arrayOfResolveResult[0].getElement() : null;
/*  78:    */   }
/*  79:    */   
/*  80:    */   @NotNull
/*  81:    */   public ResolveResult[] multiResolve(boolean paramBoolean)
/*  82:    */   {
/*  83: 82 */     RncGrammar localRncGrammar = getScope();
/*  84: 83 */     if (localRncGrammar == null)
/*  85:    */     {
/*  86: 84 */       ResolveResult[] tmp12_9 = ResolveResult.EMPTY_ARRAY;
/*  87: 84 */       if (tmp12_9 == null) {
/*  88: 84 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference", "multiResolve" }));
/*  89:    */       }
/*  90: 84 */       return tmp12_9;
/*  91:    */     }
/*  92: 87 */     Set localSet = DefinitionResolver.resolve(localRncGrammar, getCanonicalText());
/*  93: 88 */     if ((localSet == null) || (localSet.size() == 0))
/*  94:    */     {
/*  95: 88 */       ResolveResult[] tmp72_69 = ResolveResult.EMPTY_ARRAY;
/*  96: 88 */       if (tmp72_69 == null) {
/*  97: 88 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference", "multiResolve" }));
/*  98:    */       }
/*  99: 88 */       return tmp72_69;
/* 100:    */     }
/* 101: 90 */     ResolveResult[] tmp117_114 = ((ResolveResult[])ContainerUtil.map2Array(localSet, ResolveResult.class, this));
/* 102: 90 */     if (tmp117_114 == null) {
/* 103: 90 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference", "multiResolve" }));
/* 104:    */     }
/* 105: 90 */     return tmp117_114;
/* 106:    */   }
/* 107:    */   
/* 108:    */   public ResolveResult fun(Define paramDefine)
/* 109:    */   {
/* 110: 94 */     PsiElement localPsiElement = paramDefine.getPsiElement();
/* 111: 95 */     localPsiElement != null ? new PsiElementResolveResult(localPsiElement) : new ResolveResult()
/* 112:    */     {
/* 113:    */       @Nullable
/* 114:    */       public PsiElement getElement()
/* 115:    */       {
/* 116: 98 */         return null;
/* 117:    */       }
/* 118:    */       
/* 119:    */       public boolean isValidResult()
/* 120:    */       {
/* 121:101 */         return false;
/* 122:    */       }
/* 123:    */     };
/* 124:    */   }
/* 125:    */   
/* 126:    */   @Nullable
/* 127:    */   protected RncGrammar getScope()
/* 128:    */   {
/* 129:108 */     return (RncGrammar)PsiTreeUtil.getParentOfType(this.myElement, RncGrammar.class, true, new Class[] { PsiFile.class });
/* 130:    */   }
/* 131:    */   
/* 132:    */   @NotNull
/* 133:    */   public String getCanonicalText()
/* 134:    */   {
/* 135:113 */     ASTNode localASTNode = a(); String 
/* 136:114 */       tmp18_16 = (localASTNode != null ? EscapeUtil.unescapeText(localASTNode) : "");
/* 137:114 */     if (tmp18_16 == null) {
/* 138:114 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference", "getCanonicalText" }));
/* 139:    */     }
/* 140:114 */     return tmp18_16;
/* 141:    */   }
/* 142:    */   
/* 143:    */   public PsiElement handleElementRename(String paramString)
/* 144:    */     throws IncorrectOperationException
/* 145:    */   {
/* 146:118 */     ASTNode localASTNode1 = RenameUtil.createIdentifierNode(((RncRef)getElement()).getManager(), paramString);
/* 147:    */     
/* 148:120 */     ASTNode localASTNode2 = a();
/* 149:121 */     localASTNode2.getTreeParent().replaceChild(localASTNode2, localASTNode1);
/* 150:122 */     return getElement();
/* 151:    */   }
/* 152:    */   
/* 153:    */   public PsiElement bindToElement(@NotNull PsiElement paramPsiElement)
/* 154:    */     throws IncorrectOperationException
/* 155:    */   {
/* 156:126 */     if (paramPsiElement == null) {
/* 157:126 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference", "bindToElement" }));
/* 158:    */     }
/* 159:126 */     throw new UnsupportedOperationException();
/* 160:    */   }
/* 161:    */   
/* 162:    */   @NotNull
/* 163:    */   public Object[] getVariants()
/* 164:    */   {
/* 165:131 */     RncGrammar localRncGrammar = getScope();
/* 166:132 */     if (localRncGrammar == null)
/* 167:    */     {
/* 168:133 */       ResolveResult[] tmp12_9 = ResolveResult.EMPTY_ARRAY;
/* 169:133 */       if (tmp12_9 == null) {
/* 170:133 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference", "getVariants" }));
/* 171:    */       }
/* 172:133 */       return tmp12_9;
/* 173:    */     }
/* 174:136 */     Map localMap = DefinitionResolver.getAllVariants(localRncGrammar);
/* 175:137 */     if ((localMap == null) || (localMap.size() == 0))
/* 176:    */     {
/* 177:137 */       Object[] tmp68_65 = ArrayUtil.EMPTY_OBJECT_ARRAY;
/* 178:137 */       if (tmp68_65 == null) {
/* 179:137 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference", "getVariants" }));
/* 180:    */       }
/* 181:137 */       return tmp68_65;
/* 182:    */     }
/* 183:139 */     Object[] tmp125_120 = ContainerUtil.mapNotNull(localMap.values(), new Function()).toArray();
/* 184:139 */     if (tmp125_120 == null) {
/* 185:139 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference", "getVariants" }));
/* 186:    */     }
/* 187:139 */     return tmp125_120;
/* 188:    */   }
/* 189:    */   
/* 190:    */   public boolean isSoft()
/* 191:    */   {
/* 192:147 */     return false;
/* 193:    */   }
/* 194:    */   
/* 195:    */   @NotNull
/* 196:    */   public String getUnresolvedMessagePattern()
/* 197:    */   {
/* 198:152 */     String tmp2_0 = "Unresolved pattern reference ''{0}''";
/* 199:152 */     if (tmp2_0 == null) {
/* 200:152 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference", "getUnresolvedMessagePattern" }));
/* 201:    */     }
/* 202:152 */     return tmp2_0;
/* 203:    */   }
/* 204:    */   
/* 205:    */   @Nullable
/* 206:    */   public LocalQuickFix[] getQuickFixes()
/* 207:    */   {
/* 208:158 */     if (getScope() != null) {
/* 209:159 */       return new LocalQuickFix[] { new CreatePatternFix(this) };
/* 210:    */     }
/* 211:161 */     return LocalQuickFix.EMPTY_ARRAY;
/* 212:    */   }
/* 213:    */   
/* 214:    */   static class CreatePatternFix
/* 215:    */     implements LocalQuickFix
/* 216:    */   {
/* 217:    */     private final PatternReference a;
/* 218:    */     
/* 219:    */     public CreatePatternFix(PatternReference paramPatternReference)
/* 220:    */     {
/* 221:168 */       this.a = paramPatternReference;
/* 222:    */     }
/* 223:    */     
/* 224:    */     @NotNull
/* 225:    */     public String getName()
/* 226:    */     {
/* 227:174 */       String tmp30_24 = ("Create Pattern '" + this.a.getCanonicalText() + "'");
/* 228:174 */       if (tmp30_24 == null) {
/* 229:174 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference$CreatePatternFix", "getName" }));
/* 230:    */       }
/* 231:174 */       return tmp30_24;
/* 232:    */     }
/* 233:    */     
/* 234:    */     @NotNull
/* 235:    */     public String getFamilyName()
/* 236:    */     {
/* 237:179 */       String tmp2_0 = "Create Pattern";
/* 238:179 */       if (tmp2_0 == null) {
/* 239:179 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference$CreatePatternFix", "getFamilyName" }));
/* 240:    */       }
/* 241:179 */       return tmp2_0;
/* 242:    */     }
/* 243:    */     
/* 244:    */     public void applyFix(@NotNull Project paramProject, @NotNull ProblemDescriptor paramProblemDescriptor)
/* 245:    */     {
/* 246:184 */       if (paramProject == null) {
/* 247:184 */         throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference$CreatePatternFix", "applyFix" }));
/* 248:    */       }
/* 249:184 */       if (paramProblemDescriptor == null) {
/* 250:184 */         throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/intellij/plugins/relaxNG/compact/psi/impl/PatternReference$CreatePatternFix", "applyFix" }));
/* 251:    */       }
/* 252:184 */       RncFile localRncFile = (RncFile)PsiFileFactory.getInstance(((RncRef)this.a.getElement()).getProject()).createFileFromText("dummy.rnc", RncFileType.getInstance(), "dummy = xxx");
/* 253:    */       
/* 254:186 */       RncGrammar localRncGrammar1 = localRncFile.getGrammar();
/* 255:187 */       assert (localRncGrammar1 != null);
/* 256:    */       
/* 257:189 */       RncDefine localRncDefine1 = (RncDefine)localRncGrammar1.getFirstChild();
/* 258:    */       
/* 259:191 */       RncGrammar localRncGrammar2 = this.a.getScope();
/* 260:192 */       assert (localRncGrammar2 != null);
/* 261:    */       
/* 262:194 */       assert (localRncDefine1 != null);
/* 263:195 */       RncDefine localRncDefine2 = (RncDefine)localRncGrammar2.add(localRncDefine1);
/* 264:    */       
/* 265:    */ 
/* 266:198 */       localRncDefine1.setName(this.a.getCanonicalText());
/* 267:    */       
/* 268:200 */       SmartPsiElementPointer localSmartPsiElementPointer = SmartPointerManager.getInstance(paramProject).createSmartPsiElementPointer(localRncDefine2);
/* 269:    */       
/* 270:202 */       ASTNode localASTNode1 = localRncDefine2.getParent().getNode();
/* 271:203 */       assert (localASTNode1 != null);
/* 272:    */       
/* 273:205 */       ASTNode localASTNode2 = localRncDefine2.getNode();
/* 274:206 */       assert (localASTNode2 != null);
/* 275:    */       
/* 276:208 */       CodeStyleManager.getInstance(localRncDefine2.getManager().getProject()).reformatNewlyAddedElement(localASTNode1, localASTNode2);
/* 277:    */       
/* 278:210 */       RncDefine localRncDefine3 = (RncDefine)localSmartPsiElementPointer.getElement();
/* 279:211 */       assert (localRncDefine3 != null);
/* 280:    */       
/* 281:213 */       RncPattern localRncPattern = localRncDefine3.getPattern();
/* 282:214 */       assert (localRncPattern != null);
/* 283:    */       
/* 284:216 */       int i = localRncPattern.getTextRange().getStartOffset();
/* 285:    */       
/* 286:218 */       localRncPattern.delete();
/* 287:    */       
/* 288:220 */       VirtualFile localVirtualFile = ((RncRef)this.a.getElement()).getContainingFile().getVirtualFile();
/* 289:221 */       if (localVirtualFile != null) {
/* 290:222 */         FileEditorManager.getInstance(paramProject).openTextEditor(new OpenFileDescriptor(paramProject, localVirtualFile, i), true);
/* 291:    */       }
/* 292:    */     }
/* 293:    */   }
/* 294:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.compact.psi.impl.PatternReference

 * JD-Core Version:    0.7.0.1

 */